﻿using BLL;
using BLL.Comm;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Inventory.sontemplate
{
    public partial class _Product : Inventory.template.BaseInfoForm
    {
        private ProductBLL b1;
        private Product product;
        private List<Product> list;
        private List<Category> list1;



        public _Product()
        {
           

            InitializeComponent();
            b1 = new ProductBLL();
            list = new List<Product>();
            BindData();
        }
        /// <summary>
        /// 控件可操作性
        /// </summary>
        private void BindData()
        {
            dgvCpxx.DataSource = null;//清空DataGridView的数据源
            list = b1.GetList();
            dgvCpxx.DataSource  = list;//将list集合数据绑定到控件dgvCustomer作为数据源
        }
        private void ControlOpera(string opera)
        {
                tagCtxtCpmc.Enabled =
                tagCtxtPym.Enabled =
                tagCtxtGg.Enabled =
                tagCtxtCd.Enabled =
                tagCtxtSl.Enabled =
                tagCtxtGrjg.Enabled = true;
            if (opera =="U"||opera == "D")
            {
                tagCtxtCpbh.Enabled = false; 
            }
            else
                tagCtxtCpbh.Enabled = true ;

        }

        private void tagCtxtCpbh_TextChanged(object sender, EventArgs e)
        {

        }

        private void tagbtnXz_Click(object sender, EventArgs e)
        {
            ControlOpera("I");
           
        }

        private void tagbtnXg_Click(object sender, EventArgs e)
        {
            ControlOpera("U");
            ShowInfo();
        }

        private void tagbtnSc_Click(object sender, EventArgs e)
        {
            ControlOpera("D");
            ShowInfo();
        }
        /// <summary>
        ///将UI控件中的值传给Product对象，形成一条/一行记录信息 
        /// </summary>
        /// <returns></returns>
       private Product SetValue()
        {
            product = new Product();
           
            product.ProductID = tagClabCpbh.Text.Trim();
            product.ProductName = tagClabCpmc.Text.Trim();
            product.SpellingCode = tagCtxtPym.Text.Trim();
            product.BarCode = tagCtxtTxm.Text.Trim();
            product.Special = tagCtxtGg.Text.Trim();
            product.Unit = tagCtxtJldw.Text.Trim();
            product.Origin = tagCtxtCd.Text.Trim();
            product.PurchasePrice = decimal.Parse(tagCtxtGrjg.Text.Trim());
            product.SalePrice  = decimal.Parse(tagCtxtXsjg.Text.Trim());
            product.Quantity  = int .Parse(tagCtxtSl.Text.Trim());
            product.Category = new Category();
            product.Category.CategoryID  = new ProductBLL().FindCategoryIDByCategoryName (tagCtxtCplb.SelectedValue.ToString()).CategoryID ;

            return product;

        }
      

        private void gbBase_Enter(object sender, EventArgs e)
        {

        }

        private void gbBase_VisibleChanged(object sender, EventArgs e)
        {

        }

        private void tagbtnCp_VisibleChanged(object sender, EventArgs e)
        {

        }

        private void dgvCpxx_VisibleChanged(object sender, EventArgs e)
        {

        }
        private string Validation()
        {
            if (tagClabCpbh .Text.Trim() == "") { tagClabCpbh.Focus(); return "产品编号不能为空值！"; }
            if (tagClabCpmc.Text.Trim() == "") { tagClabCpmc.Focus(); return "产品名称不能为空值！"; }
            if (tagClabPym.Text.Trim() == "") { tagClabPym.Focus(); return "拼音码不能为空值！"; }
            if (tagClabTxm.Text.Trim() == "") { tagClabTxm.Focus(); return "条形码不能为空值！"; }
            if (tagClabGg.Text.Trim() == "") { tagClabGg.Focus(); return "规格不能为空值！"; }
            if (tagClabJldw.Text.Trim() == "") { tagClabJldw.Focus(); return "计量单位不能为空值！"; }
            if (tagClabCd.Text.Trim() == "") { tagClabCd.Focus(); return "产地不能为空值！"; }
            if (tagClabCplb.Text.Trim() == "") { tagClabCplb.Focus(); return "产品类别不能为空值！"; }
            if (tagClabGrjg.Text.Trim() == "") { tagClabGrjg.Focus(); return "购入价格不能为空值！"; }
            if (tagClabXsjg.Text.Trim() == "") { tagClabXsjg.Focus(); return "销售价格不能为空值！"; }
            if (tagClabSl.Text.Trim() == "") { tagClabSl.Focus(); return "数量‘’不能为空值！"; }
            return "";
        }
        private void ShowInfo()
        {
            tagCtxtCpbh.Text = dgvCpxx .CurrentRow.Cells["Bh"].Value.ToString();
            tagCtxtCpmc.Text  = dgvCpxx.CurrentRow.Cells["Cpmc"].Value.ToString();
            tagCtxtPym.Text = dgvCpxx.CurrentRow.Cells["Pym"].Value.ToString();
            tagCtxtTxm.Text = dgvCpxx.CurrentRow.Cells["Txm"].Value.ToString();
            tagCtxtGg.Text = dgvCpxx.CurrentRow.Cells["Gg"].Value.ToString();
            tagCtxtJldw.Text = dgvCpxx.CurrentRow.Cells["Jldw"].Value.ToString();
            tagCtxtCd.Text = dgvCpxx.CurrentRow.Cells["Cd"].Value.ToString();
            tagCtxtCplb.Text = dgvCpxx.CurrentRow.Cells["Splb"].Value.ToString();
            tagCtxtGrjg.Text = dgvCpxx.CurrentRow.Cells["Gjcb"].Value.ToString();
            tagCtxtXsjg.Text = dgvCpxx.CurrentRow.Cells["Xscb"].Value.ToString();
            tagCtxtSl.Text = dgvCpxx.CurrentRow.Cells["Sl"].Value.ToString();
            //tagCtxtCpbh.Text = product.ProductID.Trim();
            //tagCtxtCpmc.Text = product.ProductName.Trim();
            //tagCtxtPym.Text = product.SpellingCode.Trim();
            //tagCtxtTxm.Text = product.BarCode.Trim();
            //tagCtxtGg.Text = product.Special.Trim();
            //tagCtxtJldw.Text = product.Unit.Trim();
            //tagCtxtCd.Text = product.Origin.Trim();
            //tagCtxtGrjg.Text = product.PurchasePrice.ToString();
            //tagCtxtXsjg.Text = product.SalePrice.ToString();
            //tagCtxtSl.Text = product.Quantity.ToString();

            //tagCtxtCplb.Text = product.Category.CategoryName;

        }
       
        private void tagbtnCp_Click(object sender, EventArgs e)
        {
            
            string errorString = Validation();
            if (errorString.Length > 0) { MessageBox.Show(errorString, "系统提示"); return; }
            if (opera == Operation.INSERT)
            {
                SetValue();//获取控件值完成一行记录的对象赋值
               
                if (b1 .Insert(product))
                {
                    BindData();
                   
                    
                      
                    
                    Clear();//清空TextBox控件内容
                    MessageBox.Show("新增数据成功！", "系统提示");
                }
                else
                    MessageBox.Show("新增数据失败！", "系统提示");
            }

            if (opera == Operation.UPDATE)
            {
                SetValue();//获取控件值完成一行记录的对象赋值
                if (b1 .Update(product))
                {
                    BindData();
                    ShowInfo();//将更新数据进行控件绑定
                    MessageBox.Show("更新数据成功！", "系统提示");
                }
                else
                    MessageBox.Show("更新数据失败！", "系统提示");
            }
            if (opera == Operation.DELETE)
            {
                if (b1 .Delete(product))
                {
                    BindData();
                    Clear();//清空TextBox控件内容
                    MessageBox.Show("删除数据成功！", "系统提示");
                }
                else
                    MessageBox.Show("删除数据失败！", "系统提示");
            }
        }

        private void tagCtxtPym_TextChanged(object sender, EventArgs e)
        {
            tagCtxtPym.Text = PY.HZToSpell(tagCtxtPym.Text.Trim());
        }

        private void tagCtxtXsjg_TextChanged(object sender, EventArgs e)
        {
           
        }
        //条形码生成
        private string CreateBarCode()
        {
            //Ticks--当前实例日期和时间的周期数
            //ToFileTimeUtc--将DateTime值转算成Windows文件时间
            //哈希表取
            string val = DateTime.Now.GetHashCode().ToString() + System.Environment.TickCount;
            //截取val右边13个字符
            val = val.Substring(val.Length - 13);
            return val;
        }
        private void tagCtxtTxm_TextChanged(object sender, EventArgs e)
        {

        }

        private void tagCtxtTxm_VisibleChanged(object sender, EventArgs e)
        {
            tagCtxtTxm.Text = CreateBarCode();
        }

        private void tagCtxtCplb_VisibleChanged(object sender, EventArgs e)
        {
            DataSet cList = b1.CategoryList();
            tagCtxtCplb.DataSource = cList.Tables[0];
            tagCtxtCplb.DisplayMember = "CategoryName";
            tagCtxtCplb.ValueMember = "CategoryID";
        }

        private void tagCtxtGrjg_TextChanged(object sender, EventArgs e)
        {
            if (tagCtxtGrjg.Text.Length > 0)
             tagCtxtXsjg.Text = (double.Parse(tagCtxtGrjg.Text.Trim()) * 1.3).ToString();
        }

        private void tagCtxtCplb_SelectedIndexChanged(object sender, EventArgs e)
        {
            
           
        }
        //1：缺少下拉菜单选择后，产品类型ID值的获取方法实现
        //   事件对应下拉控件SelectedIndexChanged事件
        //2：缺少 DataGridView类别内容显示实现
        //   实现方法参考_Customer.cs的dgvCustomer_CellFormatting事件实现

    }
}
